/*
 * @Descripttion: 程序启动的时候生成菜单，并在启动后响应菜单事件
 * @version: 1.0
 * @Author: 自由如风
 * @Date: 2021-05-19 12:07:55
 * @LastEditors: 自由如风
 * @LastEditTime: 2021-05-23 17:09:50
 */

const { Plugin } = require("weixin-nodejs/dist/src");

const { wss } = require('../index')

const barrage = [
    { type: "click", name: "666", key: "666" },
    { type: "click", name: "前方高能预警，非战斗人员请立即撤离", key: "前方高能预警，非战斗人员请立即撤离" },
    { type: "click", name: "我从未见过如此厚颜无耻之人", key: "我从未见过如此厚颜无耻之人" },
    { type: "click", name: "我哭了你们呢？", key: "我哭了你们呢？" },
    { type: "click", name: "这个笑容由我来守护", key: "这个笑容由我来守护" },
]

const my = [
    { type: "click", name: "刷新二维码", key: "刷新二维码" },
    { type: "click", name: "我的弹幕间", key: "我的弹幕间" },
    { type: "click", name: "退出弹幕间", key: "退出弹幕间" },
]


class Menu extends Plugin {
    test() {
        return this.req.isEvent('CLICK')
    }
    async process() {
        // 
        if (barrage.find(e => e.key == this.params.EventKey)) {
            // 判断是否有弹幕间
            if (!!this.store.get('barrageRoom')) {
                wss.sendBullet(this.store.get('barrageRoom'), { content: barrage.find(e => e.key == this.params.EventKey).name, avatar: this.store.get('user').headimgurl, openId: this.params.FromUserName }) // 发送
            } else {
                return this.reply.text('你还未加入弹幕间，无法发送弹幕。')
            }
        } else if (my.find(e => e.key == this.params.EventKey)) {
            switch (this.params.EventKey) {
                // 刷新你二维码事件
                case my[0].key:
                    // 创建二维码
                    const res = await this.app.qrcode.create({ action_name: 'QR_LIMIT_STR_SCENE', action_info: { scene: { scene_str: this.params.FromUserName } } })
                    // 获取二维码图片
                    const qrcode = await this.app.qrcode.getUrl(res.ticket);
                    // 保存到仓库
                    this.store.set("qrcode", qrcode).save()

                    // 构建 url
                    const url = new URL('/img/qrcode.png', process.env.url)
                    return this.reply.news({ Description: "二维码有效期30天，若失效请重新获取。", Title: '邀请码', Url: this.store.get('qrcode'), PicUrl: url.toString() })

                // 弹幕间事件
                case my[1].key:
                    /**
                     * 判断是否获取自己的二维码
                     *      没有自己的二维码将导致弹幕间无法显示邀请二维码
                     */
                    if (!!this.store.get('qrcode')) {
                        const url = new URL('/dist/index.html', process.env.url)
                        url.searchParams.set('openId', this.params.FromUserName)
                        return `弹幕间地址：${url.toString()}\n请使用电脑端浏览器打开哦。`
                    } else {
                        return '请先获取二维码再获取弹幕间。'
                    }
                // 退出弹幕间
                case my[2].key:
                    this.store.set('barrageRoom', undefined).save();
                    return '已退出弹幕间，下面由"弹弹"陪你聊天。'
                default:
                    return '系统繁忙'
            }
        }
    }
}
Menu.onUse = async function (app) {
    await app.menu.create([
        {
            name: "发弹幕",
            sub_button: barrage,
        }, {
            name: "我的",
            sub_button: my
        }
    ])
}

module.exports = Menu